import {_decorator, Component, Label, Node, Sprite, SpriteFrame, tween, v3, Vec3} from 'cc';
import {GameMain} from "./GameMain";
import {AudioManager} from "./AudioManager";
import Platform, {PlatformType} from './Platform';

const { ccclass, property } = _decorator;

@ccclass('SettingUI')
export class SettingUI extends Component {

    @property({type:Node})
    targetNode:Node=null;

    @property({type:Sprite})
    musicSprite:Sprite=null;

    @property({type:Label})
    musicLabel:Label=null;

    @property({type:Sprite})
    sndSprite:Sprite=null;

    @property({type:Label})
    sndLabel:Label=null;

    @property({type:[SpriteFrame]})
    spriteFrames:Array<SpriteFrame>=[];

    onLoad()
    {

        Platform.createInsertAd();

    }

    public updateSndUI()
    {
        if(AudioManager.musicOn)
        {
            this.musicLabel.string="音乐 开";
            this.musicSprite.spriteFrame=this.spriteFrames[0];
        }
        else
        {
            this.musicLabel.string="音乐 关";
            this.musicSprite.spriteFrame=this.spriteFrames[1];
        }

        if(AudioManager.sndOn)
        {
            this.sndLabel.string="音效 开";
            this.sndSprite.spriteFrame=this.spriteFrames[2];
        }
        else
        {
            this.sndLabel.string="音效 关";
            this.sndSprite.spriteFrame=this.spriteFrames[3];
        }
    }

    public onMusicBtnClicked()
    {
        AudioManager.musicOn=!AudioManager.musicOn;
        GameMain.instance.gameData.updateMusic(AudioManager.musicOn);
        this.updateSndUI();

        if(AudioManager.musicOn)
        {
            AudioManager.playBgm();
        }
        else
        {
            AudioManager.stopBgm();
        }
    }

    public onSndBtnClicked()
    {
        AudioManager.sndOn=!AudioManager.sndOn;
        GameMain.instance.gameData.updateSnd(AudioManager.sndOn);
        this.updateSndUI();

        if(AudioManager.sndOn)
        {
            AudioManager.playKeyClip(0);
        }
    }

    public show()
    {
        if(this.node.active)
            return;
        this.updateSndUI();
        this.node.active=true;
        this.targetNode.setScale(0.4, 0.4,0.4);

        tween(this.targetNode)
            .to(0.3,{scale:Vec3.ONE}, { easing: 'backOut' })
            .start();

        if(Platform.currentPlatform===PlatformType.WECHAT || Platform.currentPlatform===PlatformType.TT)
        {
            Platform.createInsertAd();
        }
        else if(Platform.isOV())
        {
            Platform.createOVCustomAd();
        }
        else if(Platform.isAndroid())
        {
            GameMain.instance.showNativeAd();
        }
    }

    public close()
    {
        tween(this.targetNode).to(0.1,{scale:v3(0.3, 0.3, 0.3)}).call(()=>{
            this.node.active=false;
        }).start();
    }
}

